package com.opal.system.service.impl;

import com.opal.model.easyui.DataGridColumn;
import com.opal.model.easyui.EasyuiDatagridColumn;
import com.opal.model.easyui.EasyuiDatagridColumnExample;
import com.opal.system.mapper.mysql.EasyuiDatagridColumnMapper;
import com.opal.system.service.EasyuiDatagridService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EasyuiDatagridServiceImpl implements EasyuiDatagridService {
    @Autowired
    private EasyuiDatagridColumnMapper easyuiDatagridColumnMapper;

    @Override
    public Object getEasyuiDatagridColumnService(String typeName) {
        EasyuiDatagridColumn easyuiDatagridColumnCondition= new EasyuiDatagridColumn();
        easyuiDatagridColumnCondition.setTypeName(typeName);
        EasyuiDatagridColumnExample easyuiDatagridColumnExample=this.getEasyuiDatagridColumnExample(easyuiDatagridColumnCondition);
        easyuiDatagridColumnExample.setOrderByClause("field_sort asc" );
        List<EasyuiDatagridColumn> easyuiDatagridColumns = easyuiDatagridColumnMapper.selectByExample(easyuiDatagridColumnExample);
        List<List<DataGridColumn>> rList=new ArrayList<>();
        List<DataGridColumn> list = easyuiDatagridColumns.stream().map((a) -> {
            //List<DataGridColumn> l = new ArrayList<>();
            DataGridColumn dataGridColumn = new DataGridColumn();
            dataGridColumn.setAlign(a.getAlign());
            dataGridColumn.setCheckbox(a.getCheckbox());
            dataGridColumn.setColspan(a.getColspan());
            String field = a.getField();
            dataGridColumn.setField(field);
            dataGridColumn.setFixed(a.getFixed());
            dataGridColumn.setHalign(a.getHalign());
            dataGridColumn.setHidden(a.getHidden());
            dataGridColumn.setOrder(a.getOrder());
            dataGridColumn.setResizable(a.getResizable());
            dataGridColumn.setRowspan(a.getRowspan());
            dataGridColumn.setSortable(a.getSortable());
            dataGridColumn.setTitle(a.getTitle());
            dataGridColumn.setWidth(a.getWidth()+"");
            DataGridColumn.Edit edit= new DataGridColumn().new Edit();
            edit.setType(a.getEditorType());
            //
            Map<String,Object> map = new HashedMap();
//            switch (field){
//                case "empZuzhang":    //组长
//                    map.put("valueField", "empCode");          //combobox类型必须要的字段
//                    map.put("textField", "empName");           //combobox类型必须要的字段
//                    map.put("url", "/jxEmp/selectEmpByLikePostName?postName=组长");                    //combobox类型url获取参数接口
//                    //map.put("panelHeight", "auto");    //高度适应
//                    break;
//                case "empKezhang":   // 科长
//                    map.put("valueField", "empCode");          //combobox类型必须要的字段
//                    map.put("textField", "empName");           //combobox类型必须要的字段
//                    map.put("url", "/jxEmp/selectEmpByLikePostName?postName=科长");                    //combobox类型url获取参数接口
//                    //map.put("panelHeight", "auto");    //高度适应
//                    break;
//                case "empZhuguan":    // 主管
//                    map.put("valueField", "empCode");          //combobox类型必须要的字段
//                    map.put("textField", "empName");           //combobox类型必须要的字段
//                    map.put("url", "/jxEmp/selectEmpByLikePostName?postName=主管");                    //combobox类型url获取参数接口
//                    //map.put("panelHeight", "auto");    //高度适应
//                    break;
//            }

            //事件
            String onchage = a.getEditorOptionsOnchange();
            if (!StringUtils.isBlank(onchage)){
                map.put("onChange", onchage);
            }

            edit.setOptions(map);
            dataGridColumn.setEditor(edit);
            dataGridColumn.setFormatter(a.getFormatter());
            return dataGridColumn;
        }).collect(Collectors.toList());
        rList.add(list);
        return rList;
    }

    private EasyuiDatagridColumnExample getEasyuiDatagridColumnExample(EasyuiDatagridColumn easyuiDatagridColumnCondition) {
        EasyuiDatagridColumnExample easyuiDatagridColumnExample=new EasyuiDatagridColumnExample();
        EasyuiDatagridColumnExample.Criteria criteria = easyuiDatagridColumnExample.createCriteria();
        String typeName = easyuiDatagridColumnCondition.getTypeName();
        String title = easyuiDatagridColumnCondition.getTitle();
        String field = easyuiDatagridColumnCondition.getField();
        if (!StringUtils.isBlank(typeName)){
            criteria.andTypeNameEqualTo(typeName);
        }
        if (!StringUtils.isBlank(title)){
            criteria.andTitleEqualTo(title);
        }
        if (!StringUtils.isBlank(field)){
            criteria.andFieldEqualTo(field);
        }
        return easyuiDatagridColumnExample;
    }

}
